package com.xbongbong.paas.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.service.CompanyConfigService;
import com.xbongbong.paas.service.DataConnectionService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.menu.enums.ControlCenterMenuEnum;
import com.xbongbong.pro.enums.menu.enums.DataConnectionEnum;
import com.xbongbong.pro.menu.pojo.vo.ControlCenterMenuVO;
import com.xbongbong.pro.menu.pojo.vo.PaasAppMenuVO;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 数据连接
 * 创建时间 2022/1/18 4:06 PM
 * 修改时间 2022/1/18 4:06 PM
 * @author chy
 */
@Service("dataConnectionService")
public class DataConnectionServiceImpl implements DataConnectionService {

    private static final Logger LOG = LoggerFactory.getLogger(DataConnectionServiceImpl.class);


    @Resource
    private CompanyConfigService companyConfigService;

    public List<PaasAppMenuVO> getMenuList() throws XbbException {
        List<PaasAppMenuVO> dataConnectionList = new ArrayList<>();
        for(DataConnectionEnum dataConnectionEnum : DataConnectionEnum.values()){
            PaasAppMenuVO paasAppMenuVO = new PaasAppMenuVO();
            paasAppMenuVO.setName(dataConnectionEnum.getName());
            paasAppMenuVO.setAlias(dataConnectionEnum.getAlias());
            paasAppMenuVO.setAlias("cloudCode_");
            paasAppMenuVO.setIcon(dataConnectionEnum.getAccessLinkEnum().getIcon());
            paasAppMenuVO.setColor(dataConnectionEnum.getAccessLinkEnum().getColor());
            paasAppMenuVO.setUrl(dataConnectionEnum.getAccessLinkEnum().getUrl());
            Map<String, Object> valueMap = new HashMap<>();
            valueMap.put("mobileLinkAppId",dataConnectionEnum.getAccessLinkEnum().getMobileUrl());
            valueMap.put("pcLinkAppId",dataConnectionEnum.getAccessLinkEnum().getUrl());
            Map<String, Object> actionsMap = new HashMap<>();
            actionsMap.put("value",valueMap);
            actionsMap.put("type","openMenuQianKun");
            Map<String, Object> map  = new HashMap<>();
            map.put("actions",actionsMap);
            map.put("pcActions",new ArrayList<>());
            map.put("mobileActions",new ArrayList<>());
            Map<String, Object> keyMap = new HashMap<>();
            keyMap.put("key","value");
            map.put("diy_params",keyMap);
            String actions = JSONObject.toJSONString(map);
            paasAppMenuVO.setActions(actions);
            dataConnectionList.add(paasAppMenuVO);
        }
        return dataConnectionList;
    }

    @Override
    public ControlCenterMenuVO getDataConnectionMenu() throws XbbException {
        ControlCenterMenuVO controlCenterMenuVO = new ControlCenterMenuVO();
        controlCenterMenuVO.setAlias(ControlCenterMenuEnum.DATA_CONNECTION.getAlias());
        controlCenterMenuVO.setName(ControlCenterMenuEnum.DATA_CONNECTION.getName());
        controlCenterMenuVO.setIcon(ControlCenterMenuEnum.DATA_CONNECTION.getIcon());
        controlCenterMenuVO.setUrl(ControlCenterMenuEnum.DATA_CONNECTION.getUrl());
        controlCenterMenuVO.setSubMenuList(getMenuList());
        return controlCenterMenuVO;
    }

    @Override
    public boolean tplusEnable(String corpid) throws XbbException {
        String dataConnectionValue = companyConfigService.getCompanyConfigSetValue(corpid, CompanyConfigEnum.TPLUS.getAlias());
        return Objects.equals(dataConnectionValue, "1");
    }
}
